package selectCoursebl;

import java.rmi.RemoteException;
import java.util.ArrayList;

import message.Message;

import selectCourseDataService.SelectCourseDataService;
import PO.SelectPO;
import PO.UserPO;
import VO.CourseVO;
import VO.SelectVO;
import VO.UserVO;
import dataFactory.DataFactory;

public class SelectCourse {
	
	public Message selectCourse(ArrayList<SelectVO> selectList,UserVO userVO) throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		SelectCourseDataService scds=factory.getSelectCourseDataService();
		//
		ArrayList<SelectPO>poList=new ArrayList<SelectPO>();
		for(int i=0;i<selectList.size();i++){
			SelectVO selectVO=selectList.get(i);
			SelectPO selectPO=VOToPO(selectVO);
			poList.add(selectPO);
		}
		return scds.add(poList);
		
	}

	
	public Message recordScore(SelectVO selectVO, int score) throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		SelectCourseDataService scds=factory.getSelectCourseDataService();
		//
		selectVO.setScore(score);
		SelectPO selectPO=VOToPO(selectVO);
		
		return scds.update(selectPO);
		
	}

	
	public ArrayList<SelectVO> studentShowCourse(UserVO userVO) throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		SelectCourseDataService scds=factory.getSelectCourseDataService();
		//
		ArrayList<SelectPO> poList=scds.find(new UserPO(userVO.getId(),userVO.getCode(),
				userVO.getName(),userVO.getRole(),userVO.getDepart()));
		ArrayList<SelectVO> voList=new ArrayList<SelectVO>();
		for(int i=0;i<poList.size();i++){
			SelectPO po=poList.get(i);
			SelectVO vo=new SelectVO(po.getCourseNO(),po.getCourseName(),po.getTeacherName(),
					po.getStudentId(),po.getStudentName());
			voList.add(vo);
			vo.setScore(po.getScore());
		}
		
		return voList;
	}

	
	public ArrayList<SelectVO> teacherShowCourse(UserVO userVO) throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		SelectCourseDataService scds=factory.getSelectCourseDataService();
		//
		ArrayList<SelectPO> poList=scds.find(new UserPO(userVO.getId(),userVO.getCode(),userVO.getName()
				,userVO.getRole(),userVO.getDepart()));
		
		ArrayList<SelectVO> voList=new ArrayList<SelectVO>();
		for(int i=0;i<poList.size();i++){
			SelectPO po=poList.get(i);
			SelectVO vo=new SelectVO(po.getCourseNO(),po.getCourseName(),po.getTeacherName(),
					po.getStudentId(),po.getStudentName());
			vo.setScore(po.getScore());
			voList.add(vo);
		}
		
		return voList;
	}
	
	public Message quitCourse(SelectVO selectVO) throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		SelectCourseDataService scds=factory.getSelectCourseDataService();
		//
		
		return scds.delete( VOToPO(selectVO));
	}
	
	public Message start() throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		SelectCourseDataService scds=factory.getSelectCourseDataService();
		//
		return scds.start();
	}


	
	public Message close() throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		SelectCourseDataService scds=factory.getSelectCourseDataService();
		//
		return scds.close();
	}

	private SelectVO POToVO(SelectPO po){
		return new SelectVO(po.getCourseNO(),po.getCourseName(),po.getTeacherName(),
				po.getStudentId(),po.getStudentName());
	}

	private SelectPO VOToPO(SelectVO vo){
		return new SelectPO(vo.getCourseNO(),vo.getCourseName(),vo.getTeacherName(),
				vo.getStudentId(),vo.getStudentName());
	}

}
